<?php

declare(strict_types=1);

namespace App\Corp\Controller\Dashboard;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Common\Middleware\DashboardAuthMiddleware;
use App\Corp\Service\CorpService;
use App\WorkMessage\Service\WorkMessageConfigService;
use Base\Controller\AbstractController;
use Base\Request\ValidateSceneTrait;

/**
 * 企业微信授权-列表.
 *
 * Class Index.
 * @Controller
 */
class Index extends AbstractController
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var CorpService
     */
    protected $corpService;

    /**
     * @Inject
     * @var WorkMessageConfigService
     */
    protected $workMessageConfigService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/corp/index", methods="get")
     * @return array 返回数组
     */
    public function handle(): array
    {
        ## 参数验证
        //$this->validated($this->request->all());
        $corpIds[0] = $this->request->input('userInfo.corp_id');
        $tenantId = $this->request->input('userInfo.tenant_id');
        $userId = $this->request->input('userInfo.user_id');
        $deptId = $this->request->input('userInfo.dept_id');
        ## 获取当前登录用户
        //$user = user();
        ## 接收参数
        $corpName = $this->request->input('corpName', '');
        $page = $this->request->input('current', 1);
        $perPage = $this->request->input('size', 10);

        ## 组织查询条件
        $where = ['tenant_id' => $tenantId];
        $options = [
            'page' => $page,
            'perPage' => $perPage,
            'orderByRaw' => 'id desc',
        ];
        empty($corpName) || $where[] = ['name', 'LIKE', '%' . $corpName . '%'];
        ## 限定查询企业范围
        //$user['isSuperAdmin'] == 1 || $where[] = ['id', 'IN', $user['corpIds']];
        $where[] = ['corp_id', 'IN', $corpIds];
        ## 查询字段
        $columns = [
            'id',
            'name',
            'wx_corpid',
            'create_time',
        ];

        $res = $this->corpService->getCorpList($where, $columns, $options);
        ## 组织响应数据
        $data = [
            'size' => 10,
            'total' => 0,
            'pages' => 0,
            'current' => 1,
            'records' => [],
        ];
        if (empty($res['data'])) {
            return $data;
        }
        ## 获取企业聊天记录申请
        $messageConfigList = $this->workMessageConfigService->getWorkMessageConfigsByCorpId(array_column($res['data'], 'id'), ['corp_id', 'chat_status', 'chat_apply_status', 'create_time']);
        empty($messageConfigList) || $messageConfigList = array_column($messageConfigList, null, 'corpId');
        ## 处理分页数据
        $data['total'] = $res['total'];
        $data['size'] = $res['per_page'];
        $data['pages'] = $res['last_page'];
        $data['current'] = $res['current_page'];

        ## 处理数据
        foreach ($res['data'] as &$corp) {
            $corp['wxCorpId'] = $corp['wxCorpid'];
            $corp['corpId'] = $corp['id'];
            $corp['corpName'] = $corp['name'];
            $corp['chatStatus'] = isset($messageConfigList[$corp['id']]) ? $messageConfigList[$corp['id']]['chatStatus'] : 0;
            $corp['chatApplyStatus'] = isset($messageConfigList[$corp['id']]) ? $messageConfigList[$corp['id']]['chatApplyStatus'] : 0;
            $corp['messageCreatedAt'] = isset($messageConfigList[$corp['id']]) ? $messageConfigList[$corp['id']]['createTime'] : '';
            unset($corp['id'], $corp['name']);
        }
        $data['records'] = $res['data'];

        return $data;
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [];
    }
}
